/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package actions;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Object which fires changes in the enabled state of actions.  An implementation
 * of this is supplied when constructing an ActionManager.  The simplest form is
 * to use SimpleActionController, which simply has setters for the enabled and
 * selected states for actions.
 * <p>
 * Other implementations are possible, which use more complex contextual 
 * information from the application, such as selection, and don't require 
 * application code to make explicit calls to enable or disable actions (consider
 * NetBeans' global context which contains some objects, and actions which 
 * are enabled or disabled based on the presence or absence of an object of
 * a given type in the context at any given time).
 * <p>
 * @see SimpleActionController
 *
 * @author Timothy Boudreau
 */
public abstract class ActionController {
    public abstract boolean isEnabled (String id);
    public abstract boolean isSelected (String id);
    public abstract Set <String> getTogglableActions();
    public abstract boolean isTogglable (String id);


    private List <ActionStateListener> listeners = new ArrayList();
    final void addListener(ActionStateListener listener) {
        listeners.add (listener);
    }
    
    protected final void fireSelectedChanged (String id, boolean val) {
        for (ActionStateListener l : listeners) {
            l.selectionChanged(id, val);
        }
    }
    
    protected final void fireEnabledChanged (String id, boolean val) {
        for (ActionStateListener l : listeners) {
            l.enablementChanged(id, val);
        }
    }
}
